/**
 *******************************************************************************
 * @FileName  : system_call.c
 * @Author    : GaoQiu
 * @CreateDate: 2020-05-18
 * @Copyright : Copyright(C) GaoQiu
 *              All Rights Reserved.
 *******************************************************************************
 *
 * The information contained herein is confidential and proprietary property of
 * GaoQiu and is available under the terms of Commercial License Agreement
 * between GaoQiu and the licensee in separate contract or the terms described
 * here-in.
 *
 * This heading MUST NOT be removed from this file.
 *
 * Licensees are granted free, non-transferable use of the information in this
 * file under Mutual Non-Disclosure Agreement. NO WARRENTY of ANY KIND is provided.
 *
 *******************************************************************************
 */
#include <stdint.h>
#include <errno.h>
#include <sys/stat.h>

extern int __io_putchar(char ch) __attribute__((weak));
extern int __io_getchar(void) __attribute__((weak));

/**************************************************************************************************
  Functions
**************************************************************************************************/

/*************************************************************************************************/
/*!
 *  \brief      Open file.
 *
 *  \param      name        Filename.
 *  \param      flags       Flags.
 *  \param      mode        Open mode.
 *
 *  \return     File handle.
 */
/*************************************************************************************************/
int _open(const char *name, int flags, int mode)
{
  /* No implementation. */
  (void)name;
  (void)flags;
  (void)mode;
  return -1;
}

/*************************************************************************************************/
/*!
 *  \brief      Close file.
 *
 *  \param      file        File handle.
 *
 *  \return     Result.
 */
/*************************************************************************************************/
int _close(int file)
{
  /* No implementation. */
  (void)file;
  return 0;
}

/*************************************************************************************************/
/*!
 *  \brief      File status.
 *
 *  \param      file        File handle.
 *  \param      *st         File status response.
 *
 *  \return     Result.
 */
/*************************************************************************************************/
int _fstat(int file, struct stat *st)
{
  /* No implementation. */
  (void)file;
  (void)st;
  return 0;
}

/*************************************************************************************************/
/*!
 *  \brief      Get terminal type.
 *
 *  \param      file        File handle.
 *
 *  \return     Result.
 */
/*************************************************************************************************/
int _isatty(int file)
{
  /* No implementation. */
  (void)file;
  return 1;
}

/*************************************************************************************************/
/*!
 *  \brief      File seek.
 *
 *  \param      file        File handle.
 *  \param      ptr         Offset.
 *  \param      dir         Direction of offset.
 *
 *  \return     Result.
 */
/*************************************************************************************************/
int _lseek(int file, int ptr, int dir)
{
  /* No implementation. */
  (void)file;
  (void)ptr;
  (void)dir;
  return 0;
}


__attribute__((weak)) int _read(int file, char *ptr, int len)
{
    (void)file;
    int DataIdx;
    for (DataIdx = 0; DataIdx < len; DataIdx++)
    {
        *ptr++ = __io_getchar();
    }
    return len;
}

__attribute__((weak)) int _write(int file, char *ptr, int len)
{
    (void)file;
    int DataIdx;
    for (DataIdx = 0; DataIdx < len; DataIdx++)
    {
        __io_putchar(*ptr++);
    }
    return len;
}

#if 0
/*************************************************************************************************/
/*!
 *  \brief      Change the program's break limit.
 *
 *  \param      incr        Size.
 *
 *  \return     Old break value.
 */
/*************************************************************************************************/
caddr_t _sbrk(int incr)
{
	extern uint8_t *sysHeapStart;
	extern uint32_t sysHeapSize;

	caddr_t addr = (caddr_t)sysHeapStart;

	/* Round up to nearest multiple of 4 for word alignment */
	incr = (incr + 3) & ~3;
	sysHeapStart += incr;
	sysHeapSize -= incr;
	return addr;
}
#endif
